package com.example.beikeyigou.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.beikeyigou.R;

public class EditDialog extends Dialog implements View.OnClickListener {
    private TextView negtiveTv ,positiveTv, titleTv;
    private EditText editText;


    public EditDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit);

        findView();

        negtiveTv.setOnClickListener(this);
        positiveTv.setOnClickListener(this);
    }

    /**
     * 初始化控件
     */
    private void findView() {
        titleTv = findViewById(R.id.dialog_edit_title);
        negtiveTv = findViewById(R.id.dialog_edit_confirm);
        positiveTv = findViewById(R.id.dialog_edit_cancel);
        editText = findViewById(R.id.dialog_edit);
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public EditDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_edit_cancel:
                if (onClickBottomListener!= null) {
                    onClickBottomListener.onNegtiveClick();
                }
                break;
            case R.id.dialog_edit_confirm:
                if (onClickBottomListener!= null) {
                    onClickBottomListener.onPositiveClick();
                }
                break;
        }
    }

    public interface OnClickBottomListener{
        //点击确定按钮事件
        public void onPositiveClick();
        //点击取消按钮事件
        public void onNegtiveClick();
    }

    public EditDialog setDialogWidth(){
        Window window = getWindow();   //当前屏幕窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();  //获取窗口信息参数
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int)(d.getWidth());   //对话框窗口宽度为屏幕窗口宽度
        wlp.gravity = Gravity.BOTTOM;    //从底部弹出对话框
        window.setBackgroundDrawableResource(android.R.color.transparent);   //设置窗口背景透明
        window.setAttributes(wlp);
        //自动弹出软键盘
        handler.sendEmptyMessageDelayed(1,100);
        return this;
    }

    public String getTitleTv() {
        return titleTv.getText().toString();
    }

    public EditDialog setMyTitle(String title) {
        titleTv.setText(title);
        return this;
    }

    public String getEditText() {
        return editText.getText().toString();
    }

    public EditDialog setEditText(String editText) {
        this.editText.setText(editText);
        return this;
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };
}
